<?php

namespace common\models;

use Yii;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%picture}}".
 *
 * @property string $id
 * @property string $path
 * @property string $md5
 * @property string $create_time
 * @property integer $status
 */
class Picture extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%picture}}';
    }

    /**
     * 根据图片id获取url
     * @param $id string|int
     * @return mixed
     */
    public static function getUrlById($id)
    {
        return self::find()->select('url')->where(['id' => $id])->one()['url'];
    }

    /**
     * 根据图片url获取id
     * @param $url string
     * @return mixed
     */
    public static function getIdByUrl($url)
    {
        return self::find()->select('id')->where(['url' => $url])->one()['id'];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type', 'url', 'surl', 'sha1', 'vinfo',], 'string'],
            [['create_time', 'status', 'source', 'crop'], 'integer'],
            [['path'], 'string', 'max' => 255],
            [['md5'], 'string', 'max' => 32],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'path' => '路径',
            'md5' => '文件md5',
            'create_time' => '创建时间',
            'status' => '状态',
            'url' => '链接',
            'sha1' => 'sha1编码',
            'type' => '类型',
            'vinfo' => '类型'
        ];
    }
}
